package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.param.BaseParam;
import com.mall.order.IOrderListService;

import com.mall.order.IOrederDetailService;
import com.mall.order.dto.*;

import com.mall.order.OrderCoreService;
import com.mall.order.constant.OrderRetCode;

import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Description: 获取当前用户的所有订单
 * @Author: WD created on 2021-09-29 17:34
 * @Version:
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class ShoppingOrderController {

    @Reference(timeout = 30000, retries = 2, check = false)
    IOrderListService orderListService;

    @Reference(timeout = 30000, retries = 2, check = false)
    OrderCoreService orderCoreService;

    @Reference(timeout = 30000, check = false)
    IOrederDetailService orederDetailService;


    @GetMapping("/order")
    public ResponseData showUserOrders(BaseParam param, HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        OrderListRequest request = new OrderListRequest();
        request.setSize(param.getSize());
        request.setPage(param.getPage());
        request.setUserId(uid);
        OrderListResponse response = orderListService.queryOrderList(request);

        List<OrderDetailInfo> infoList = response.getDetailInfoList();
        Long total = response.getTotal();

        OrderListVO orderListVO = new OrderListVO(infoList, total);

        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(orderListVO);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }

    @GetMapping("/order/{id}")
    public ResponseData showOrderDetail(@PathVariable("id") String id, HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        NewOrderDetailRequest request = new NewOrderDetailRequest();
        request.setOrderId(id);
        request.setUserId(uid);
        NewOrderDetailResponse response = orederDetailService.showOrderDetail(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getNewOrderDetailDto());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * HWX
     * 创建订单
     */
    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject jsonObject = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(jsonObject.get("uid").toString());

        request.setUserId(uid);
//        GlobalIdGeneratorUtil globalIdGeneratorUtil = null;
        request.setUniqueKey(UUID.randomUUID().toString().replace("-", ""));
        //调用服务，创建订单
        CreateOrderResponse response = orderCoreService.createOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * HWX
     * 取消订单
     */
    @PostMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map map, HttpServletRequest servletRequest) {

        String orderId = (String) map.get("orderId");
        CancelOrderRequest request = new CancelOrderRequest();
        request.setOrderId(orderId);
        CancelOrderResponse response = orderCoreService.cancelOrder(request);
        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id) {
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(id);
        DeleteOrderResponse response = orederDetailService.deleteOrder(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}